Exercise 1

TreeImage

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
## ✓ tibble  3.0.4     ✓ dplyr   1.0.2
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggtree)
## Registered S3 method overwritten by 'treeio':
##   method     from
##   root.phylo ape
## ggtree v2.2.4  For help: https://yulab-smu.github.io/treedata-book/
## 
## If you use ggtree in published research, please cite the most appropriate paper(s):
## 
## - Guangchuang Yu. Using ggtree to visualize data on tree-like structures. Current Protocols in Bioinformatics, 2020, 69:e96. doi:10.1002/cpbi.96
## - Guangchuang Yu, Tommy Tsan-Yuk Lam, Huachen Zhu, Yi Guan. Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution 2018, 35(12):3041-3043. doi:10.1093/molbev/msy194
## - Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution 2017, 8(1):28-36. doi:10.1111/2041-210X.12628
## 
## Attaching package: 'ggtree'
## The following object is masked from 'package:tidyr':
## 
##     expand
library(treeio)
## treeio v1.12.0  For help: https://yulab-smu.github.io/treedata-book/
## 
## If you use treeio in published research, please cite:
## 
## LG Wang, TTY Lam, S Xu, Z Dai, L Zhou, T Feng, P Guo, CW Dunn, BR Jones, T Bradley, H Zhu, Y Guan, Y Jiang, G Yu. treeio: an R package for phylogenetic tree input and output with richly annotated and associated data. Molecular Biology and Evolution 2020, 37(2):599-603. doi: 10.1093/molbev/msz240
library(ggimage)

Exercise 2

TOL_output.mafft

tt <- read.tree("data/fastree_result.tre")
tt
## 
## Phylogenetic tree with 21 tips and 19 internal nodes.
## 
## Tip labels:
##   Archaeoglobus_fulgidus, Trypanosoma_cruzi_nuclear, Amphidinium_carterae, Saccharomyces_cerevisiae_nuclear, Homo_sapies_nuclear, Drosophila_yakuba_nuclear, ...
## Node labels:
##   , 0.999, 0.856, 1.000, 0.988, 0.979, ...
## 
## Unrooted; includes branch lengths.
ggtree(tt) + geom_tiplab()

Exercise 3

library(tidyverse)
library(ggtree)
library(treeio)
library(ggimage)
tree <- read.tree("data/tree_newick.nwk")
tree
## 
## Phylogenetic tree with 13 tips and 12 internal nodes.
## 
## Tip labels:
##   A, B, C, D, E, F, ...
## 
## Rooted; includes branch lengths.
ggplot(tree) + geom_tree() + theme_tree()

ggtree(tree)

adding a scale

ggtree(tree) + geom_treescale()

ggtree(tree) + theme_tree2()

ggtree(tree, branch.length="none")

ggtree(tree, branch.length="none", color="blue", size=2, linetype=3)

Exercise 1
ggtree(tree, layout = "slanted")

ggtree(tree, layout = "circular")

ggtree(tree, branch.length="none", color="red", size=2, linetype=1)

#———————————————————————————————–

Other tree genomes

# create the basic plot
p <- ggtree(tree)
Node points, tip points and tiplab
p + geom_nodepoint() + geom_tippoint() + geom_tiplab()

Exercise 2
p + geom_tippoint(shape = "diamond", color = "purple") + geom_tiplab(color = "purple") + geom_nodepoint(color = "yellow", alpha = 0.5, size = 3)

Tree annotation

ggtree(tree) + geom_text(aes(label=node), hjust=-.1)

ggtree(tree) + geom_tiplab()

Labelling Clades

ggtree(tree) + 
  geom_cladelabel(node=17, label="Random", color="red", offset =.8, align = TRUE)+
  geom_cladelabel(node=21, label="Different", 
                  color="blue", offset=.8, align = TRUE) +
 theme_tree2() + 
  xlim(0, 70) 

removing x-axis scale

ggtree(tree) + 
  geom_cladelabel(node=17, label="Random", color="red", offset =.8, align = TRUE)+
  geom_cladelabel(node=21, label="Different", 
                  color="blue", offset=.8, align = TRUE) +
 theme_tree2() + 
  xlim(0, 70) + 
  theme_tree()

highlight the entire clade

ggtree(tree) + 
  geom_tiplab() + 
  geom_hilight(node=17, fill="gold") + 
  geom_hilight(node=21, fill="purple")

ggtree(tree) + geom_tiplab()

MRCA(tree, c("C", "E"))
## [1] 17
MRCA(tree, c("G", "H"))
## [1] 21

Connecting Taxa

Read the data

tree <- read.beast("data/flu_tree_beast.tree")
tree
## 'treedata' S4 object that stored information of
##  'data/flu_tree_beast.tree'.
## 
## ...@ phylo: 
## Phylogenetic tree with 76 tips and 75 internal nodes.
## 
## Tip labels:
##   A/Hokkaido/30-1-a/2013, A/New_York/334/2004, A/New_York/463/2005, A/New_York/452/1999, A/New_York/238/2005, A/New_York/523/1998, ...
## 
## Rooted; includes branch lengths.
## 
## with the following features available:
##  'height',   'height_0.95_HPD',  'height_median',    'height_range', 'length',
##  'length_0.95_HPD',  'length_median',    'length_range', 'posterior',    'rate',
##  'rate_0.95_HPD',    'rate_median',  'rate_range'.

Scale Bar

ggtree(tree, mrsd="2013-01-01") + 
  theme_tree2() 

Finally, add tip labels and adjust axis

ggtree(tree, mrsd="2013-01-01") + 
  theme_tree2() + 
  geom_tiplab(align=TRUE, linesize=.5) + 
  xlim(1990, 2020)

msaplot(p=ggtree(tree), fasta="data/flu_aasequence.fasta", window=c(150, 175))

set.seed(42)
trees <- lapply(rep(c(10, 25, 50, 100), 3), rtree)
class(trees) <- "multiPhylo"
ggtree(trees) + facet_wrap(~.id, scale="free", ncol=4) + ggtitle("Phylogenetic trees")

tree <- rtree(30)
p <- ggtree(tree)
d1 <- data.frame(id=tree$tip.label, val=rnorm(30, sd=3))
p2 <- facet_plot(p, panel="dot", data=d1, geom=geom_point, aes(x=val), color='red3')
d2 <- data.frame(id=tree$tip.label, value = abs(rnorm(30, mean=100, sd=50)))
p3 <- facet_plot(p2, panel='bar', data=d2, geom=geom_segment, 
           aes(x=0, xend=value, y=y, yend=y), size=3, color='blue4') 
p3 + theme_tree2()


Tree File

tree <- read.tree("data/fastree_result.tre")
tree
## 
## Phylogenetic tree with 21 tips and 19 internal nodes.
## 
## Tip labels:
##   Archaeoglobus_fulgidus, Trypanosoma_cruzi_nuclear, Amphidinium_carterae, Saccharomyces_cerevisiae_nuclear, Homo_sapies_nuclear, Drosophila_yakuba_nuclear, ...
## Node labels:
##   , 0.999, 0.856, 1.000, 0.988, 0.979, ...
## 
## Unrooted; includes branch lengths.
ggtree(tree) + 
  theme_tree2() +
    geom_tiplab() + 
  geom_nodelab() + 
  xlim(0,2) 

ggtree(tree, layout = "circular") + 
  theme_tree2() +
    geom_tiplab() + 
  geom_nodelab() + 
  xlim(0,36)

Exercise 4

library(ggtree)

Output_tree.nhx

nhxfile <- read.tree("data/Output_Tree.nhx")
nhxfile
## 
## Phylogenetic tree with 21 tips and 19 internal nodes.
## 
## Tip labels:
##   Oryza_mitochondrion, Rhizobium_leguminosarum, Escherichia_coli_, Burkholderia_cenocepacia_, Chlorobium_chlorochromatii, Oryza_chloroplast, ...
## 
## Unrooted; includes branch lengths.
ggtree(nhxfile) + geom_text(aes(label=node), hjust=-.3)

ggtree(nhxfile) +
    geom_nodelab()+
  geom_tiplab(hjust=-.1, size = 3) +
    geom_hilight(node=23, fill="gold", extend = 0.5) + 
    geom_hilight(node=37, fill="purple", extend = 0.8) + 
    geom_hilight(node=27, fill="blue", extend = 0.5) 

xlim(0,36)
## <ScaleContinuousPosition>
##  Range:  
##  Limits:    0 --   36
ggtree(nhxfile, layout = "circular") +
    geom_nodelab()+
  geom_tiplab(hjust=-.1, size = 3) +
    geom_hilight(node=23, fill="gold", extend = 0.5) + 
    geom_hilight(node=37, fill="purple", extend = 0.5) + 
    geom_hilight(node=27, fill="blue", extend = 0.5) 

xlim(0,36)
## <ScaleContinuousPosition>
##  Range:  
##  Limits:    0 --   36